(function() {
    angular.module('BlurAdmin.pages.shops')
        .controller('shopsCtrl', shopsCtrl);

    function shopsCtrl($scope, $datepicker, $ShareData, $http, $filter, $myconfig, $uibModal, taRegisterTool, $openModal, $mydialog) {
        //弹窗框
        $scope.open = $openModal; //创建实例化对象
        function fn(val) { //关闭模态框方法
            $scope.myopen.opt_close = val;
            $scope.opt_close = val;
        }
        $scope.myopen = function(page, size) { //打开模态框
            $scope.open.newOpen(page, size, $scope, fn, 'static');
        };

        //分页
        // $scope.items_page = 5; //每页多少条
        // $scope.maxSize = 5; //底下显示几个页
        // $scope.bigTotalItems = 50; //一共多少数据
        // $scope.bigCurrentPage = 1; //当前是第几页

        $scope.pageChanged = function(val) {
            var Page_num = val //选中的页面数
            $scope.list_data.page = val;
            $scope.search_data();
        };


        $scope.list_data = {
            page: 1,
            limit: 10,
            name: "",
        };
        $scope.search_data = function() { //点击搜索按钮
            list_num();
        };

        //列表数据统计
        function list_num() {
            var url = $myconfig.api.COUNT_SHOP;
            var opt = {
                "name": $scope.list_data.name
            }

            $http.get(url, { params: opt })
                .then(function(res) {
                    $scope.bigTotalItems = res.data.data ? res.data.data : 0;
                    list_data();
                });
        }

        //列表数据
        function list_data() {
            var oo = new Date();
            console.log(oo.getTime());

            $http.get($myconfig.api.LIST_SHOP, { params: $scope.list_data })
                .then(function(res) {
                    if (res.data.success) {
                        for (var i = 0; i < res.data.data.length; i++) {
                            res.data.data[i].num = (($scope.list_data.page - 1) * $scope.list_data.limit) + i + 1;
                        }
                        $scope.list_data_opt = res.data.data
                    } else {
                        $mydialog.fail(res.data.data);
                    }
                });

        }
        $scope.search_data();

        //新增店铺
        $scope.xinzeng = function() {
            $scope.shop = null;
            $scope.is_update = false;
            $scope.myopen('app/pages/shops/addShop.html', 'lg');
        }

        //编辑店铺
        $scope.edit = function(val) {
            $scope.shop = val;
            $scope.is_update = true;
            $scope.myopen('app/pages/shops/addShop.html', 'lg');
        }


        //删除商铺单条商铺
        $scope.remove_shop = function() {
            $http.get($myconfig.api.REMOVE_SHOP, {
                    params: {
                        shop_id: shop_id
                    }
                })
                .then(function(res) {
                    if (res.data.success) {
                        $mydialog.success('删除成功');
                        $scope.search_data();
                    } else {
                        $mydialog.fail(res.data.data);
                    }

                })
        };
        //确认删除
        var shop_id = null;

        function my_callback(val) {
            if (val == true) {
                $scope.remove_shop();
            }
        };

        $scope.fn_delete = function(val) {
            shop_id = val;
            $mydialog.inquiry('确定要删除吗？', my_callback)
        };
    }
})();